<?php require_once("../../../oh.framework/oh.framework.php"); ?>
<html>
<head>
<?php echo oh("math")->head(); ?>
<?php echo oh("css")->link("style.css"); ?>
<?php echo oh("script")->src("matherax.js"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
</head>
<body onload="javascript:body_onload();">
<div id="bar">
matherax
</div>
<div id="page">
	<div id="document">
		<?php
			require_once("html.php");
			require_once("io.php");			
			require_once("other.php");			
						
			if (isset($_POST['task'])) {
				if ($_POST['task'] == "save") {					
					$edit = $_POST['block'];
					$level = $_POST['level'];
					$tmp = mload("sample.math");				
					$block = 0;
					$doc = array();
					$new = true;
					foreach($tmp as $key=>$value) {
						$value = trim($value);
						if ($value != "") {
							if ($block == $edit) {
								$doc[$key] = $_POST['data'];
								$new = false;
							} else {
								$doc[$key] = $value;
							}
							$block++;
						}
					}
					if ($new) $doc[] = $_POST['data'];
					msave("sample.math",$doc);
				}
				if ($_POST['task'] == "close") {					
				}
				if ($_POST['task'] == "delete") {					
					$edit = $_POST['block'];
					$level = $_POST['level'];
					$tmp = mload("sample.math");				
					$block = 0;
					$doc = array();
					$new = true;
					foreach($tmp as $key=>$value) {
						$value = trim($value);
						if ($value != "") {
							if ($block == $edit) {
								$doc[$key] = "";
								$new = false;
							} else {
								$doc[$key] = $value;
							}
							$block++;
						}
					}
					//if ($new) $doc[] = $_POST['data'];
					msave("sample.math",$doc);
				}
				if ($_POST['task'] == "close") {					
				}
			}
			
			
			$doc = mload("sample.math");
			$doc[] = "(modifica)";
						
			$id = array();
			$block = 0;
			$edit = isset($_GET['edit']) ? (int)$_GET['edit'] : -1;

			$index = array();
			
			foreach($doc as $key=>$value) {				
				$key = explode(":",$key);
				$key = $key[0];
				$value = trim($value);
				if ($value != "") {
					if (false) {
					} else if ($key=="capitolo") {
						$index = mup(1,$index);
						if ($edit == $block) {
							echo '<h1>'.mindex(1,$index).' '.minput($value,$block).'</h1>';										
						} else {
							echo '<h1>'.mindex(1,$index).' '.mlink($value,$block).'</h1>';
						}
					} else if ($key=="paragrafo") {
						$index = mup(2,$index);
						if ($edit == $block) {
							echo '<h2>'.mindex(2,$index).' '.minput($value,$block).'</h2>';										
						} else {
							echo '<h2>'.mindex(2,$index).' '.mlink($value,$block).'</h2>';
						}
					} else if ($key=="sottoparagrafo") {
						$index = mup(3,$index);
						if ($edit == $block) {
							echo '<h3>'.mindex(3,$index).' '.minput($value,$block).'</h2>';										
						} else {
							echo '<h3>'.mindex(3,$index).' '.mlink($value,$block).'</h2>';
						}
					} else {
						if ($edit == $block) {
							echo '<p>'.mtextarea($value,$block).'</p>';
						} else {
							echo '<p>'.mlink($value,$block).'</p>';					
						}	
					}
					$block++;
				}
			}
		?>					
	</div>
	<div id="toc">
		<div class="inner">
			<div class="title">Indice</div>
			<?php
				$doc = mload("sample.math");
				$index = array();
				$block = 0;
				foreach($doc as $key=>$value) {				
					$key = explode(":",$key);
					$key = $key[0];
					$value = trim($value);
					if ($value != "") {
						if (false) {
						} else if ($key=="capitolo") {
							$index = mup(1,$index);
							echo '<div class="h1">'.mindex(1,$index).' '.mlink($value,$block).'</div>';
						} else if ($key=="paragrafo") {
							$index = mup(2,$index);
							echo '<div class="h2">'.mindex(2,$index).' '.mlink($value,$block).'</div>';
						} else if ($key=="sottoparagrafo") {
							$index = mup(3,$index);
							echo '<div class="h3">'.mindex(3,$index).' '.mlink($value,$block).'</div>';
						} 
						$block++;
					}
				}
			?>
		</div>
	</div>
	<div class="clr"></div>
</div>
</body>
</html>
